package com.cskaoyan.controller.wx;


import com.cskaoyan.aliyun.AliyunUtils;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.vo.user.LoginUserVO;
import com.cskaoyan.bean.vo.user.UserInfoVO;
import com.cskaoyan.bean.wx.WXRegisterUser;
import com.cskaoyan.service.wx.WXAuthService;
import com.cskaoyan.service.wx.WXCouponService;
import com.cskaoyan.token.MarketToken;

import com.cskaoyan.util.MD5Util;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Bowen Deng
 * @since 2022/06/28 16:41
 */
@RestController
@RequestMapping("wx/auth")
public class WXAuthController {

    @Autowired
    AliyunUtils aliyunUtils;

    @Autowired
    WXAuthService wxAuthService;

    @Autowired
    WXCouponService wxCouponService;


    /**
     * wx user login
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/29 12:58 上午
     */
    @RequestMapping("login")
    public BaseRespVo login(@RequestBody Map map) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");

        Integer status = wxAuthService.isAvailable(username);
        if(status!=0){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(401);
            baseRespVo.setErrmsg("用户状态不可用");
            return baseRespVo;
        }
        Subject subject = SecurityUtils.getSubject();
        MarketToken marketToken = new MarketToken(username, MD5Util.MD5EncodeUtf8(password), "wx");
        try {
            subject.login(marketToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }

        if (subject.isAuthenticated()) {
            MarketUser primaryPrincipal = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            LoginUserVO loginUserVO = new LoginUserVO();
            UserInfoVO userInfoVO = new UserInfoVO();
            loginUserVO.setToken((String) subject.getSession().getId());
            userInfoVO.setNickName(primaryPrincipal.getNickname());
            userInfoVO.setAvatarUrl(primaryPrincipal.getAvatar());
            loginUserVO.setUserInfo(userInfoVO);
            return BaseRespVo.ok(loginUserVO);
        }
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(401);
        baseRespVo.setErrmsg("登陆失败，请重试");
        return baseRespVo;
    }

    /**
     * 登出
     *
     * @return com.cskaoyan.bean.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/29 9:22 下午
     */
    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    /**
     * 短信验证
     *
     * @return
     * @author Bowen Deng
     * @since 2022/06/29 9:22 下午
     */
    @RequestMapping("regCaptcha")
    public BaseRespVo regCaptcha(@RequestBody Map map, HttpServletRequest request) {
        String code = RandomStringUtils.randomNumeric(6);
        String phoneNumber = (String) map.get("mobile");
        aliyunUtils.sendMsg(phoneNumber, code);
        wxAuthService.insertCode(phoneNumber,code);
        return BaseRespVo.ok();
    }

    /**
     * 用户注册
     * 根据前台传入的数据注册
     * * 705:手机号已注册
     * * 703:验证码错误
     * * 704:用户名已注册
     *
     * @param registerUser
     * @return com.cskaoyan.bean.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/29 9:37 下午
     */
    @RequestMapping("register")
    public BaseRespVo register(@RequestBody WXRegisterUser registerUser) {
        Integer code = wxAuthService.register(registerUser);
        if (code == 705) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(705);
            baseRespVo.setErrmsg("手机号已注册");
            return baseRespVo;
        }

        if (code == 703) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(703);
            baseRespVo.setErrmsg("验证码错误");
            return baseRespVo;
        }

        if (code == 704) {
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(704);
            baseRespVo.setErrmsg("用户名已注册");
            return baseRespVo;
        }

        Subject subject = SecurityUtils.getSubject();
        MarketToken marketToken = new MarketToken(registerUser.getUsername(), MD5Util.MD5EncodeUtf8(registerUser.getPassword()), "wx");
        try {
            subject.login(marketToken);
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        LoginUserVO loginUserVO = new LoginUserVO();
        UserInfoVO userInfoVO = new UserInfoVO();
        MarketUser marketUser = wxAuthService.queryUserByName(registerUser.getUsername());
        userInfoVO.setAvatarUrl(marketUser.getAvatar());
        userInfoVO.setNickName(marketUser.getNickname());
        loginUserVO.setUserInfo(userInfoVO);
        String token = subject.getSession().getId().toString();
        loginUserVO.setToken(token);
        return BaseRespVo.ok(loginUserVO);
    }

    /**
     * 703:验证码错误
     *
     * @param map
     * @return com.cskaoyan.bean.vo.BaseRespVo
     * @author Bowen Deng
     * @since 2022/06/30 9:07 上午
     */
    @RequestMapping("reset")
    public BaseRespVo reset(@RequestBody Map map) {
        String code = (String) map.get("code");
        String mobile = (String) map.get("mobile");
        String password = (String) map.get("password");
        Integer errno = wxAuthService.reset(code,mobile,MD5Util.MD5EncodeUtf8(password));
        if(errno==703){
            BaseRespVo baseRespVo = new BaseRespVo();
            baseRespVo.setErrno(703);
            baseRespVo.setErrmsg("验证码错误");
            return baseRespVo;
        }
        return BaseRespVo.ok();
    }
}
